package com.aube.mis.service.impl;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aube.api.v2.service.advertising.AdvertisingVoV2Service;
import com.aube.api.v2.service.app.model.AppModelVoV2Service;
import com.aube.api.v2.service.businessModel.BusinessModelVoV2Service;
import com.aube.api.v2.service.source.SourceRelatedVoV2Service;
import com.aube.api.v2.vo.advertising.req.AdvertisingQueryReqVo;
import com.aube.api.v2.vo.advertising.resp.AdvertisingVo;
import com.aube.api.v2.vo.businessModel.resp.BusinessModelRespVo;
import com.aube.api.v2.vo.businessModel.resp.TemplateBusinessModelRespVo;
import com.aube.apiv2.vo.app.template.ClientTmplateRespVo;
import com.aube.apiv2.vo.app.template.TmplateConfigRespVo;
import com.aube.apiv2.vo.app.template.model.AppModelConfigRespVo;
import com.aube.apiv2.vo.app.template.model.ModelStyleRespVo;
import com.aube.common.req.vo.PaginationReqVo;
import com.aube.mis.controller.ShowController;
import com.aube.mis.dto.AdvertisingDTO;
import com.aube.mis.dto.AppModelConfigDTO;
import com.aube.mis.dto.AppModelStyleDTO;
import com.aube.mis.dto.ModelDTO;
import com.aube.mis.dto.ShowModelDTO;
import com.aube.mis.dto.TemplateDTO;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.ShowSettingService;
import com.aube.mis.util.JsonUtil;
import com.aube.mis.vo.DataTableCriterias;
import com.aube.support.ResultCode;

@Service
public class ShowSettingServiceImpl extends AbstractService implements ShowSettingService {

	private static Logger log = LoggerFactory.getLogger(ShowController.class);

	@Resource
	BusinessModelVoV2Service businessModelService;

	@Resource
	AdvertisingVoV2Service advertisingService;

	@Resource
	AppModelVoV2Service appModelVoV2Service;

	@Resource
	SourceRelatedVoV2Service sourceRelatedVoV2Service;

	@SuppressWarnings("unchecked")
	@Override
	public List<AppModelConfigDTO> getAppModelConfigList(String showId, String templateCode, String clientType) {
		ResultCode<List<AppModelConfigRespVo>> result = appModelVoV2Service.getAppModelConfigList(Long.parseLong(showId), clientType, templateCode);
		List<AppModelConfigDTO> resultList = (List<AppModelConfigDTO>) CollectionUtils.collect(result.getRetval(), new Transformer() {

			@Override
			public AppModelConfigDTO transform(Object object) {
				return convertAppModelConfigDTO((AppModelConfigRespVo) object);
			}
		});
		return resultList;
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<ShowModelDTO> getModelList(final String showId) {
		ResultCode<List<ClientTmplateRespVo>> templateConfigList = appModelVoV2Service.getAppTmplateConfigList(Long.parseLong(showId));
		PaginationReqVo<TemplateBusinessModelRespVo> reqVo = new PaginationReqVo<>();
		reqVo.setShowid(showId);
		reqVo.setPage(0);
		reqVo.setMaxnum(9999);
		ResultCode<List<TemplateBusinessModelRespVo>> modelVoList = businessModelService.queryList(reqVo);

		final List<TemplateDTO> templateResultList = (List<TemplateDTO>) CollectionUtils.collect(modelVoList.getRetval(), new Transformer() {

			@Override
			public TemplateDTO transform(Object object) {
				return convertTemplateDTO((TemplateBusinessModelRespVo) object);
			}
		});
		List<ShowModelDTO> resultList = (List<ShowModelDTO>) CollectionUtils.collect(templateConfigList.getRetval(), new Transformer() {

			@Override
			public ShowModelDTO transform(Object object) {
				ClientTmplateRespVo vo = (ClientTmplateRespVo) object;
				ShowModelDTO result = new ShowModelDTO();
				final String clientType = vo.getClientType();
				result.setClientType(clientType);
				result.setClientName(vo.getClientName());
				List<TmplateConfigRespVo> templateList = vo.getTemplateList();
				List<TemplateDTO> resultList = (List<TemplateDTO>) CollectionUtils.collect(templateList, new Transformer() {

					@Override
					public TemplateDTO transform(Object object) {
						TmplateConfigRespVo vo = (TmplateConfigRespVo) object;
						TemplateDTO result = new TemplateDTO();
						result.setTemplateCode(vo.getCode());
						result.setTemplateName(vo.getDesc());
						for (TemplateDTO templateDTO : templateResultList) {
							if (templateDTO.getClientType().equals(clientType) && templateDTO.getTemplateCode().equals(vo.getCode())) {
								result.setModelList(templateDTO.getModelList());
								break;
							}
						}
						return result;
					}
				});
				result.setTemplateList(resultList);
				return result;
			}
		});
		return resultList;
	}

	@Override
	public void sortModelList(String showId, List<String> modelIdList) {
		businessModelService.orderBusinessModel(showId, modelIdList);
	}

	@Override
	public ModelDTO getModelDetail(String showId, String modelId) {
		ResultCode<BusinessModelRespVo> result = businessModelService.getDetailById(showId, modelId);
		return convertModelDTO(result.getRetval());
	}

	@Override
	public ModelDTO editModel(ModelDTO modelDTO) {
		BusinessModelRespVo vo = new BusinessModelRespVo();
		vo.setMaxAppVersion(modelDTO.getMaxAppVersion());
		vo.setMiniAppVersion(modelDTO.getMinAppVersion());
		vo.setModelCode(modelDTO.getModelCode());
		vo.setModelDesc(modelDTO.getModelDesc());
		vo.setModelName(modelDTO.getModelName());
		vo.setTopPicture(modelDTO.getTopPicUrl());
		vo.setTemplateCode(modelDTO.getTemplateCode());
		vo.setWhitelist(modelDTO.getWhiteList());
		vo.setModelId(modelDTO.getModelId());
		vo.setSortNum(modelDTO.getSortNum());
		vo.setOstype(modelDTO.getOsType());
		vo.setShowid(modelDTO.getShowId());
		vo.setStyleId(modelDTO.getStyleId());
		vo.setRelateIds(modelDTO.getRelatedIds());
		vo.setClientType(modelDTO.getClientType());
		if(StringUtils.isNotEmpty(modelDTO.getModelId())) {
			ResultCode<BusinessModelRespVo> result = businessModelService.getDetailById(modelDTO.getShowId(), modelDTO.getModelId());
			vo.setExtraDescMap(result.getRetval().getExtraDescMap());
		}
		ResultCode<BusinessModelRespVo> result = businessModelService.saveOrModify(vo);
		return convertModelDTO(result.getRetval());
	}

	@Override
	public void deleteModel(String showId, String modelId) {
		businessModelService.removeById(showId, modelId);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<AdvertisingDTO> getAdvertisingList(DataTableCriterias criterias, String showId, String clientType, String templateCode) {
		PaginationReqVo<AdvertisingQueryReqVo> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);
		AdvertisingQueryReqVo req = new AdvertisingQueryReqVo();
		req.setClientType(clientType);
		req.setTemplateCode(templateCode);
		reqVo.setReq(req);
		ResultCode<List<AdvertisingVo>> list = advertisingService.queryList(reqVo);
		List<AdvertisingDTO> resultList = (List<AdvertisingDTO>) CollectionUtils.collect(list.getRetval(), new Transformer() {

			@Override
			public AdvertisingDTO transform(Object object) {
				return convertAdvertisingDTO((AdvertisingVo) object);
			}
		});
		return resultList;
	}

	@Override
	public AdvertisingDTO getAdvertisingDetail(String showId, String advertisingId) {
		ResultCode<AdvertisingVo> result = advertisingService.getDetailById(showId, advertisingId);
		return convertAdvertisingDTO(result.getRetval());
	}

	@Override
	public void editAdvertising(AdvertisingDTO advertisingDTO) {
		AdvertisingVo vo = new AdvertisingVo();
		vo.setAdType(advertisingDTO.getType());
		vo.setAdvertisingid(advertisingDTO.getAdvertisingId());
		vo.setBeginTimeStamp(new Timestamp(advertisingDTO.getBeginTime()));
		vo.setEndTimeeStamp(new Timestamp(advertisingDTO.getEndTime()));
		vo.setNewDesc(advertisingDTO.getDesc());
		vo.setNewPicurl(advertisingDTO.getPicUrl());
		vo.setNewTitle(advertisingDTO.getTitle());
		vo.setShowid(advertisingDTO.getShowId());
		vo.setSortNum(advertisingDTO.getSortNum());
		vo.setRelatedId(advertisingDTO.getRelatedId());
		vo.setClientType(advertisingDTO.getClientType());
		vo.setNewRemark(advertisingDTO.getRemark());
		vo.setTemplateCode(advertisingDTO.getTemplateCode());
		advertisingService.saveOrModify(vo);
	}

	@Override
	public void deleteAdvertising(String showId, String advertisingId) {
		advertisingService.removeById(showId, advertisingId);
	}
	
	@Override
	public void relateAdvertising(String advertisingId, String relatedId) {
		advertisingService.saveAdType(advertisingId, "video", relatedId);
	}
	
	@Override
	public void sortAdvertisingVideo(String showId, String clientType, String templateCode, String videoIds) {
		advertisingService.saveVideoCollections(showId, templateCode, clientType, Arrays.asList(videoIds.split(",")));
	}
	
	@Override
	public List<String> getAdvertisingRelatedVideo(String showId, String clientType, String templateCode) {
		ResultCode<List<String>> result = advertisingService.getVideoCollections(showId, templateCode, clientType);
		return result.getRetval();
	}
	
	@Override
	public List<String> getModelIdsByResource(String showId, String resourceType, String resourceId) {
		ResultCode<List<String>> result = businessModelService.getRelatedIdsByResource(showId, resourceId, resourceType);
		return result.getRetval();
	}
	
	@Override
	public void updateRelatedIdsByResource(String showId, List<String> modelIds, String resourceId) {
		businessModelService.setRelatedIdsWithResource(modelIds, resourceId, showId);
	}
	
	@Override
	public void saveExtraDesc(String showId, String modelId, String extraDescJson) {
		ResultCode<BusinessModelRespVo> result = businessModelService.getDetailById(showId, modelId);
		BusinessModelRespVo businessModelRespVo = result.getRetval();
		businessModelRespVo.setExtraDescMap((Map<String, String>)JsonUtil.json2Obj(extraDescJson, Map.class));
		businessModelService.saveOrModify(businessModelRespVo);
	}

	@SuppressWarnings("unchecked")
	private TemplateDTO convertTemplateDTO(TemplateBusinessModelRespVo vo) {
		TemplateDTO result = new TemplateDTO();
		List<ModelDTO> modelList = (List<ModelDTO>) CollectionUtils.collect(vo.getModelList(), new Transformer() {

			@Override
			public ModelDTO transform(Object object) {
				return convertModelDTO((BusinessModelRespVo) object);
			}
		});
		result.setModelList(modelList);
		result.setTemplateCode(vo.getTemplateCode());
		result.setTemplateName(vo.getTemplateName());
		result.setClientType(vo.getClientType());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private AppModelConfigDTO convertAppModelConfigDTO(AppModelConfigRespVo vo) {
		AppModelConfigDTO result = new AppModelConfigDTO();
		result.setModelId(vo.getId().toString());
		result.setModelCode(vo.getModelCode());
		result.setModelName(vo.getModelName());
		result.setRelatedType(vo.getRelateType());
		if (CollectionUtils.isNotEmpty(vo.getStyleList())) {
			List<AppModelStyleDTO> styleList = (List<AppModelStyleDTO>) CollectionUtils.collect(vo.getStyleList(), new Transformer() {

				@Override
				public AppModelStyleDTO transform(Object object) {
					ModelStyleRespVo vo = (ModelStyleRespVo) object;
					AppModelStyleDTO result = new AppModelStyleDTO();
					result.setStyleId(vo.getStyleId());
					result.setStyleName(vo.getModelStyle());
					result.setStyleDesc(vo.getStyleDesc());
					return result;
				}
			});
			result.setStyleList(styleList);
		}
		return result;
	}

	private ModelDTO convertModelDTO(BusinessModelRespVo vo) {
		ModelDTO result = new ModelDTO();
		result.setMaxAppVersion(vo.getMaxAppVersion());
		result.setMinAppVersion(vo.getMiniAppVersion());
		result.setModelCode(vo.getModelCode());
		result.setModelDesc(vo.getModelDesc());
		result.setModelName(vo.getModelName());
		result.setTopPicUrl(vo.getTopPicture());
		result.setTemplateCode(vo.getTemplateCode());
		result.setWhiteList(vo.getWhitelist());
		result.setModelId(vo.getModelId());
		result.setOsType(vo.getOstype());
		result.setStyleId(vo.getStyleId());
		result.setSortNum(vo.getSortNum());
		result.setRelatedIds(vo.getRelateIds());
		result.setShowId(vo.getShowid());
		result.setRelatedType(vo.getRelatedType());
		result.setClientType(vo.getClientType());
		result.setExtraDescJson(JsonUtil.toJson(vo.getExtraDescMap()));
		return result;
	}

	private AdvertisingDTO convertAdvertisingDTO(AdvertisingVo vo) {
		AdvertisingDTO result = new AdvertisingDTO();
		if (vo.getBeginTimeStamp() != null) {
			result.setBeginTime(vo.getBeginTimeStamp().getTime());
		}
		if (vo.getEndTimeeStamp() != null) {
			result.setEndTime(vo.getEndTimeeStamp().getTime());
		}
		result.setDesc(vo.getNewDesc());
		result.setTitle(vo.getNewTitle());
		result.setType(vo.getAdType());
		result.setSortNum(vo.getSortNum());
		result.setShowId(vo.getShowid());
		result.setRelatedId(vo.getRelatedId());
		result.setPicUrl(vo.getNewPicurl());
		result.setAdvertisingId(vo.getAdvertisingid());
		result.setClientType(vo.getClientType());
		result.setRemark(vo.getNewRemark());
		result.setTemplateCode(vo.getTemplateCode());
		return result;
	}
}
